import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users'
import Rights from '../components/power/Rights'
import Roles from '../components/power/Roles'
import Cate from '../components/good/Cate'
import Params from '../components/good/Params'
import List from '../components/good/List'
import Add from '../components/good/Add'
import Edit from '../components/good/Edit'
import Order from '../components/order/Order'
import Report from '../components/report/Report'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/goods/*',
    redirect: '/*'
  },
  {
    path: '/home',
    component: Home,
    redirect: '/Welcome',
    children: [{
      path: '/welcome',
      component: Welcome
    },
    {
      path: '/users',
      component: Users
    },
    {
      path: '/rights',
      component: Rights
    }, {
      path: '/roles',
      component: Roles
    },
    {
      path: '/categories',
      component: Cate
    }, {
      path: '/params',
      component: Params
    },
    {
      path: '/goods',
      component: List
    },
    {
      name: 'Add',
      path: '/goods/add',
      component: Add
    },
    {
      name: 'Edit',
      path: '/goods/edit',
      component: Edit
    },
    {
      path: '/orders',
      component: Order
    },
    {
      path: '/reports',
      component: Report
    }
    ]
  }

]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作(放行的函数) next('/login')强制跳转login
router.beforeEach((to, from, next) => {
  // 访问登录页，放行
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token, 强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})
// 把router暴露出去
export default router
